from typing import List


class Solution:
    def isMonotonic(self, nums: List[int]) -> bool:
        flag, ans = 0, True
        for i in range(1, len(nums)):
            if flag == 0:
                if nums[i] > nums[i - 1]:
                    flag = 1 # 顺序
                elif nums[i] < nums[i - 1]:
                    flag = -1 # 倒序
            else:
                if flag == 1 and nums[i] < nums[i - 1]:
                    return False
                if flag == -1 and nums[i] > nums[i - 1]:
                    return False
        return ans